﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;

//引用命名空间
using System.Web.Script.Serialization;
using System.Web.Security;
using Utility;
using Utilty;
using WanDa.Model;


namespace Demo.Controllers
{
    public class BaseController : Controller
    {
        private  users _loginInfo;

        //private static readonly Logger logger = LogManager.GetCurrentClassLogger();

        public users LoginInfo
        {
            get
            {
                if (HttpContext.User.Identity.IsAuthenticated)
                {
                    var strUserData = ((FormsIdentity)User.Identity).Ticket.UserData;
                    _loginInfo = new JavaScriptSerializer().Deserialize<users>(strUserData);
                    if (_loginInfo == null)
                    {
                        _loginInfo = RedisHelper.Get<users>(strUserData);
                    }
                }
                return _loginInfo;
            }
        }

        public static void WriteDataToCookie(users userData)
        {
            //将实体对象序列化为json字符串
            var strUserData = new JavaScriptSerializer().Serialize(userData);

            //将json字符串生成对应的令牌数据
            var ticket = new FormsAuthenticationTicket(1, userData.userid.ToString(), DateTime.Now, DateTime.Now.AddHours(12), false, strUserData);
            var ticketVal = FormsAuthentication.Encrypt(ticket);

            CookiesHelper.SetCookie(FormsAuthentication.FormsCookieName, ticketVal, ticket.Expiration);
        }

        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            filterContext.Controller.ViewBag.LoginInfo = LoginInfo;
            base.OnActionExecuting(filterContext);
        }

        //protected override void OnException(ExceptionContext filterContext)
        //{
        //    if (filterContext == null)
        //    {
        //        base.OnException(null);
        //        return;
        //    }
        //    var httpException = filterContext.Exception as HttpException;

        //    //http错误跳出在globa中处理
        //    if (httpException != null) return;

        //    var controllerName = filterContext.RouteData.Values["controller"].ToString();
        //    var actionName = filterContext.RouteData.Values["action"].ToString();
        //    var timeStamp = filterContext.HttpContext.Timestamp;
        //    var routeId = string.Empty;
        //    if (filterContext.RouteData.Values["id"] != null)
        //    {
        //        routeId = filterContext.RouteData.Values["id"].ToString();
        //    }

        //    //记录异常信息到 /common/Log.txt
        //    var message = new StringBuilder();
        //    message.Append(Environment.NewLine);
        //    message.Append("请求信息：");
        //    message.Append(Environment.NewLine);
        //    message.AppendFormat("Controller={0}，Action={1}，RouteId={2}，TimeStamp={3}", controllerName, actionName, routeId, timeStamp);
        //    message.Append(Environment.NewLine);
        //    message.Append("异常信息：");
        //    message.Append(Environment.NewLine);
        //    message.Append(filterContext.Exception);
        //    logger.Debug(message.ToString());
        //}
    }
}